package com.zwxbc.funplay.servlet;

import com.zwxbc.funplay.entity.Player;
import com.zwxbc.funplay.service.PlayerService;
import com.zwxbc.funplay.service.impl.PlayerServiceImpl;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
@WebServlet(name = "PlayerInfoServlet", value = "/PlayerInfoServlet")
public class PlayerInfoServlet extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("UTF-8");
		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		// 单个文件大小限制10M
		upload.setFileSizeMax(10 * 1024 * 1024);
		// 总文件大小限制 50M
		upload.setSizeMax(50 * 1024 * 1024);
		// 对中文文件编码处理
		upload.setHeaderEncoding("UTF-8");
		Player player = new Player();
		PlayerService playerService = new PlayerServiceImpl();
		try {
			List<FileItem> fileItemList = upload.parseRequest(request);

			for (FileItem fileItem : fileItemList) {
				if (fileItem.isFormField()) {
					String name = fileItem.getFieldName();
					// 使用同样的编码处理方式防止乱码
					String value = new String(fileItem.getString().getBytes("ISO-8859-1"), "UTF-8");
					if ("username".equalsIgnoreCase(name)) {
						player.setPlayerUsername(value);
					} else if ("name".equalsIgnoreCase(name)) {
						player.setPlayerName(value);
					} else if ("email".equalsIgnoreCase(name)) {
						player.setPlayerEmail(value);
					} else if ("contact".equalsIgnoreCase(name)) {
						player.setPlayerContact(value);
					}
				} else {
					//文件不为空
					if (fileItem.getSize() > 0) {
						//获取图片存储路径
						String path = this.getServletContext().getRealPath("/img/user");
						String fileName = fileItem.getName();
						File file = new File(path, fileName);

						if (!file.getParentFile().exists()) {
							//确保目录存在，如果不存在则创建
							file.getParentFile().mkdirs();
						}
						if (!file.exists()) {
							//文件不存在时才写入
							fileItem.write(file);
						}
						//将文件名写入头像字段
						player.setPlayerImage(fileName);
					}
				}
			}
			//如果没有上传头像，使用以前头像
			if (player.getPlayerImage() == null) {
				String photo = ((Player) request.getSession().getAttribute("user")).getPlayerImage();
				player.setPlayerImage(photo);
			}

			//保存用户信息至数据库
			boolean success = playerService.playerUpdate(player);
			Player playerByUsername = playerService.getPlayerByUsername(player.getPlayerUsername());
			//更新当前会话中用的户信息对象
			HttpSession session = request.getSession();
			session.setAttribute("user", playerByUsername);
			session.setAttribute("message", "信息已成功更新");

			request.getRequestDispatcher(request.getContextPath() + "/view/player/playerInfo.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
